package com.ss.android.article.base.feature.category.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.category.a.CategoryManager;
import com.ss.android.article.base.feature.main.IRecentFragment;
import com.ss.android.article.base.feature.model.CategoryItem;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.main.BaseHomePageFragment;
import com.ss.android.common.ui.view.TextDrawable;
import com.ss.android.j.ClickableBackgroundCompat;

import java.util.LinkedHashMap;

public class CategoryTabStrip extends HorizontalScrollView {
    private static boolean D;

    static {
        CategoryTabStrip.D = false;
    }

    private final CategoryTabStrip$b m;
    public ViewPager.OnPageChangeListener a;
    public LinearLayout b;
    ViewPager c;
    int d;
    int e;
    float f;
    boolean g;
    float h;
    boolean i;
    CategoryTabStrip$d j;
    private int A;
    private LayoutInflater mLayoutInflater;
    private TextDrawable[] C;
    private View.OnClickListener E;
    private LinearLayout.LayoutParams k;
    private LinearLayout.LayoutParams l;
    private Style n;
    private Paint o;
    private Rect p;
    private Rect q;
    private Paint r;
    private int s;
    private int t;
    private int u;
    private int v;
    private boolean w;
    private int x;
    private LinkedHashMap y;
    private int z;

    public CategoryTabStrip(Context arg2) {
        this(arg2, null);
    }

    public CategoryTabStrip(Context arg2, AttributeSet arg3) {
        this(arg2, arg3, 0);
    }

    public CategoryTabStrip(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        int v0 = 0;
        this.m = new CategoryTabStrip$b(this, (byte) 0);
        this.n = Style.Light;
        this.e = 0;
        this.f = 0f;
        this.p = new Rect();
        this.q = new Rect();
        this.s = 10;
        this.t = 0;
        this.u = 0;
        this.w = true;
        this.i = false;
        this.y = new LinkedHashMap();
        this.C = new TextDrawable[3];
        this.E = new View.OnClickListener() {

            public final void onClick(View arg7) {
                CategoryTabStrip.CategoryTabStrip$d v1;
                int v0 = ((CategoryTabStrip.c) arg7.getTag()).d;
                if (CategoryTabStrip.this.j == null || CategoryTabStrip.this.c.getCurrentItem() != v0) {
                    CategoryTabStrip.this.g = true;
                    if (CategoryTabStrip.this.j == null) {
                        return;
                    }

                    v1 = CategoryTabStrip.this.j;
                    v1.a.t = 2;
                    v1.a.o = 1;
                    v1.a.p = false;
                    v1.a.mViewPager.setCurrentItem(v0, false);
                    v1.a.mMotionDirectionFrameLayout.a();
                } else {
                    v1 = CategoryTabStrip.this.j;
                    BaseHomePageFragment v2 = v1.a;
                    IRecentFragment v0_1 = null;
                    if (v2.mSubscriptionPagerAdapter != null) {
                        v0_1 = v2.mSubscriptionPagerAdapter.d();
                    }

                    if (v0_1 != null) {
                        v0_1.handleRefreshClick(1);
                    }

                    v1.a.mMotionDirectionFrameLayout.a();
                }
            }
        };
        CategoryManager.getInstance(context);
        this.mLayoutInflater = LayoutInflater.from(context);
        this.setWillNotDraw(false);
        this.b = new LinearLayout(context);
        this.b.setOrientation(LinearLayout.HORIZONTAL);
        this.b.setLayoutParams(new FrameLayout.LayoutParams(-2, -1));
        this.addView(this.b);
        this.z = ((int) UIUtils.dp2px(this.getContext(), 3f));
        UIUtils.dp2px(this.getContext(), 4f);
        this.A = ((int) UIUtils.dp2px(this.getContext(), 6f));
        DisplayMetrics v1 = this.getResources().getDisplayMetrics();
        this.s = ((int) TypedValue.applyDimension(1, ((float) this.s), v1));
        this.t = ((int) TypedValue.applyDimension(1, ((float) this.t), v1));
        this.o = new Paint(1);
        this.o.setStyle(Paint.Style.FILL);
        this.r = new Paint();
        this.r.setAntiAlias(true);
        this.r.setStyle(Paint.Style.FILL);
        this.k = new LinearLayout.LayoutParams(-2, -1);
        this.l = new LinearLayout.LayoutParams(-2, -1);
        this.l.leftMargin = this.t;
        while (v0 < this.C.length) {
            this.C[v0] = new TextDrawable(this.getContext());
            ++v0;
        }
    }

    static TextView a(View arg2) {
        TextView v0;
        TextView v1 = null;
        if (arg2 == null) {
            v0 = v1;
        } else {
            Object v0_1 = (arg2.getTag() instanceof c) ? arg2.getTag() : v1;
            if (v0_1 == null) {
                return v1;
            }

            v0 = ((c) v0_1).a;
        }

        return v0;
    }

    public static boolean b() {
        return CategoryTabStrip.D;
    }

    public final void a() {
        Object v2_1 = null;
        View v0_1 = null;
        com.ss.android.l.c.c();
        this.b.removeAllViews();
        LinkedHashMap v6 = new LinkedHashMap(this.y);
        this.y.clear();
        this.d = this.c.getAdapter().getCount();
        PagerAdapter v1 = this.c.getAdapter();
        int v3;
        for (v3 = 0; v3 < this.d; ++v3) {
            Object v0 = v6.remove(((a) v1).b(v3).category);
            CharSequence v7 = v1.getPageTitle(v3);
            CategoryItem v8 = ((a) v1).b(v3);
            if (v0 == null) {
                View v5 = this.mLayoutInflater.inflate(com.ss.android.article.browser.R.layout.bg, ((ViewGroup) this), false);
                c v2 = new c();
                v2.a = v5.findViewById(com.ss.android.article.browser.R.id.n8);
                v2.b = v5.findViewById(com.ss.android.article.browser.R.id.n9);
                v2.c = v8;
                v5.setTag(v2);
                v2.a.setGravity(17);
                v2.a.setSingleLine();
                v5.setFocusable(true);
                v0_1 = v5;
                v2_1 = v2;
            } else {
                v2_1 = ((View) v0).getTag();
            }

            if (this.h > 0f) {
                ((c) v2_1).a.setTextSize(this.h);
            } else {
                this.h = ((c) v2_1).a.getTextSize();
            }

            ((c) v2_1).d = v3;
            if ((AppData.getInstance().U()) && AppData.getInstance().B() == 1 || (this.n == Style.Search || this.n == Style.SearchDark)) {
                v0_1.setPadding(this.z, 0, this.z, 0);
                ((c) v2_1).a.setPadding(this.A, 0, this.A, 0);
            }

            if (this.i) {
                TextView v8_1 = ((c) v2_1).a;
                float v5_1 = this.e == v3 ? this.h + 1f : this.h;
                v8_1.setTextSize(v5_1);
            }

            ((c) v2_1).a.setText(v7);
            v0_1.setOnClickListener(this.E);
            if (v3 == 0) {
                this.b.addView(v0_1, v3, this.k);
            } else {
                this.b.addView(v0_1, v3, this.l);
            }
        }

        this.d();
        this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @SuppressLint(value = {"NewApi"})
            public final void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16) {
                    CategoryTabStrip.this.getViewTreeObserver().removeGlobalOnLayoutListener(((ViewTreeObserver.OnGlobalLayoutListener) this));
                } else {
                    CategoryTabStrip.this.getViewTreeObserver().removeOnGlobalLayoutListener(((ViewTreeObserver.OnGlobalLayoutListener) this));
                }

                CategoryTabStrip.this.e = CategoryTabStrip.this.c.getCurrentItem();
                CategoryTabStrip.this.a(CategoryTabStrip.this.e);
            }
        });
        com.ss.android.l.c.c();
    }

    final void a(int arg11) {
        float v8 = 1f;
        if (this.d != 0 && (!this.g || arg11 == this.c.getCurrentItem())) {
            Rect v2 = this.p;
            View v3 = this.b.getChildAt(this.e);
            TextView v4 = CategoryTabStrip.a(v3);
            if (v4 != null) {
                float v1 = ((float) (v3.getLeft() + v4.getLeft()));
                float v0 = (((float) v4.getWidth())) + v1;
                if (this.f > 0f && this.e < this.d - 1) {
                    View v5 = this.b.getChildAt(this.e + 1);
                    TextView v6 = CategoryTabStrip.a(v5);
                    if (v6 != null) {
                        float v5_1 = ((float) (v5.getLeft() + v6.getLeft()));
                        float v6_1 = (((float) v6.getWidth())) + v5_1;
                        v1 = v1 * (v8 - this.f) + v5_1 * this.f;
                        v0 = v0 * (v8 - this.f) + this.f * v6_1;
                        v2.set((((int) v1)) + this.getPaddingLeft(), this.getPaddingTop() + v3.getTop() + v4.getTop(), (((int) v0)) + this.getPaddingLeft(), v3.getTop() + this.getPaddingTop() + v4.getTop() + v4.getHeight());
                    } else {
//                        goto label_74;
                    }
                } else {
                    v2.set((((int) v1)) + this.getPaddingLeft(), this.getPaddingTop() + v3.getTop() + v4.getTop(), (((int) v0)) + this.getPaddingLeft(), v3.getTop() + this.getPaddingTop() + v4.getTop() + v4.getHeight());
                }
            }

//            label_74:
            int v0_1 = this.u;
            if (this.p.left < this.getScrollX() + this.s) {
                v0_1 = this.p.left - this.s;
            } else if (this.p.right > this.getScrollX() + (this.getWidth() - this.b.getPaddingRight()) - this.s) {
                v0_1 = this.p.right - (this.getWidth() - this.b.getPaddingRight()) + this.s;
            }

            if (v0_1 != this.u) {
                this.scrollTo(v0_1, 0);
                this.u = v0_1;
            } else if ((AppData.getInstance().getAbSettings().isBackRefreshInCategoryAll()) && this.x - arg11 >= 6 && arg11 == 0) {
                this.scrollTo(v0_1, 0);
            }

            this.x = arg11;
        }
    }

    private int b(int arg3) {
        int v0 = arg3 >= this.b.getChildCount() ? 0 : (this.b.getChildAt(arg3).getLeft() + this.b.getChildAt(arg3).getRight()) / 2;
        return v0;
    }

    public final void b(View arg8) {
        Drawable v6 = null;
        int v5 = R.color.ssxinzi8;
        int v3 = R.drawable.fj;
        int v4 = R.drawable.fi;
        Object v0 = arg8.getTag();
        if (v0 != null && ((c) v0).c != null) {
            ImageView v2 = ((c) v0).b;
            int v1 = ((c) v0).c.tip_new ? VISIBLE : INVISIBLE;
            v2.setVisibility(v1);
            if (this.n == Style.Light) {
                ((c) v0).a.setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi2));
                if (((c) v0).b.getVisibility() == View.VISIBLE) {
                    ((c) v0).b.setImageResource(v4);
                }

                ClickableBackgroundCompat.setClickableBorderlessBackground(arg8);
                return;
            }

            if (this.n == Style.Dark) {
                ((c) v0).a.setTextColor(UIUtils.b(this.getResources().getColor(v5)));
                if (((c) v0).b.getVisibility() == View.VISIBLE) {
                    ((c) v0).b.setImageResource(v3);
                }

                arg8.setBackgroundDrawable(v6);
                return;
            }

            if (this.n == Style.SearchDark) {
                ((c) v0).a.setTextColor(this.getResources().getColor(v5));
                if (((c) v0).b.getVisibility() == View.VISIBLE) {
                    ((c) v0).b.setImageResource(v3);
                }

                arg8.setBackgroundDrawable(v6);
                return;
            }

            if (this.n != Style.Search) {
                return;
            }

            ((c) v0).a.setTextColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi1));
            if (((c) v0).b.getVisibility() == View.VISIBLE) {
                ((c) v0).b.setImageResource(v4);
            }

            ClickableBackgroundCompat.setClickableBorderlessBackground(arg8);
        }
    }

    private void c() {
        this.d();
        this.invalidate();
    }

    private void d() {
        int v0;
        for (v0 = 0; v0 < this.d; ++v0) {
            this.b(this.b.getChildAt(v0));
        }
    }

    public void draw(Canvas arg12) {
        String v2_1;
        int v10 = R.color.ssxinzi8;
        int v9 = R.color.ssxinzi4;
        super.draw(arg12);
        CategoryTabStrip.D = true;
        int v0;
        for (v0 = 0; v0 < this.b.getChildCount(); ++v0) {
            if (v0 >= this.e - 1 && v0 <= this.e + 1) {
                View v3 = this.b.getChildAt(v0);
                TextView v4 = CategoryTabStrip.a(v3);
                if (v4 != null) {
                    TextDrawable v5 = this.C[v0 - this.e + 1];
                    int v6 = arg12.save();
                    arg12.clipRect(this.p);
                    v5.a(TypedValue.applyDimension(0, v4.getTextSize(), v5.a.getDisplayMetrics()));
                    v5.a(v4.getTypeface());
                    CharSequence v2 = v4.getText();

                    v5.b = v2 == null ? "" : v2;
                    v5.a();
                    if (this.n == Style.Light) {
                        v5.a(this.getResources().getColor(v9));
                    } else if (this.n == Style.Dark) {
                        v5.a(this.getResources().getColor(v10));
                    } else if (this.n == Style.SearchDark) {
                        v5.a(this.getResources().getColor(v10));
                    } else if (this.n == Style.Search) {
                        v5.a(this.getResources().getColor(v9));
                    }

                    int v2_2 = v3.getLeft() + v4.getLeft() + (v4.getWidth() - v5.getIntrinsicWidth()) / 2 + this.getPaddingLeft();
                    int v3_1 = v3.getTop() + v4.getTop() + v4.getPaddingTop() + this.getPaddingTop();
                    this.q.set(v2_2, v3_1, v5.getIntrinsicWidth() + v2_2, v5.getIntrinsicHeight() + v3_1);
                    v5.setBounds(this.q);
                    v5.draw(arg12);
                    arg12.restoreToCount(v6);
                }
            }
        }

        v0 = arg12.save();
        arg12.translate(((float) this.getScrollX()), 0f);
        arg12.restoreToCount(v0);
    }

    public int getLastFullVisibleChildPosition() {
        int v1 = this.b.getChildCount() - 1;
        int v0 = 0;
        while (true) {
            if (v0 >= this.b.getChildCount()) {
                break;
            } else if (this.b.getChildAt(v0).getRight() > this.getWidth() - this.getPaddingLeft()) {
                --v0;
            } else {
                ++v0;
                continue;
            }

            return Math.max(1, v0);
        }

        v0 = v1;
        label_15:
        return Math.max(1, v0);
    }

    int getScrollRange() {
        int v0 = 0;
        if (this.getChildCount() > 0) {
            v0 = Math.max(0, this.getChildAt(0).getWidth() - (this.getWidth() - this.getPaddingLeft() - this.getPaddingRight() - this.b.getPaddingRight()));
        }

        return v0;
    }

    protected void onDraw(Canvas arg7) {
        if (this.w) {
            try {
                int v0_1 = this.getHeight() - (((int) UIUtils.dp2px(this.getContext(), 2.5f)));
                View v1 = this.b.getChildAt(this.e);
                if (v1 == null) {
                    return;
                }

                int v3 = ((int) ((((float) ((v1.getRight() + v1.getLeft()) / 2 - (((int) UIUtils.dp2px(this.getContext(), 12f)))))) + this.f * (((float) (this.b(this.e + 1) - this.b(this.e))))));
                if (this.n == Style.Light) {
                    this.r.setColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi4));
                } else if (this.n == Style.Dark) {
                    this.r.setColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi8));
                } else if (this.n == Style.SearchDark) {
                    this.r.setColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi8));
                } else if (this.n == Style.Search) {
                    this.r.setColor(this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi4));
                }

                arg7.drawRect(((float) v3), ((float) v0_1), (((float) v3)) + UIUtils.dp2px(this.getContext(), 24f), UIUtils.dp2px(this.getContext(), 1.5f) + (((float) v0_1)), this.r);
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
    }

    public void onLayout(boolean arg1, int arg2, int arg3, int arg4, int arg5) {
        super.onLayout(arg1, arg2, arg3, arg4, arg5);
    }

    public void onMeasure(int arg1, int arg2) {
        super.onMeasure(arg1, arg2);
    }

    public void onRestoreInstanceState(Parcelable arg2) {
        super.onRestoreInstanceState(((SavedState) arg2).getSuperState());
        this.e = ((SavedState) arg2).currentPosition;
        this.requestLayout();
    }

    public Parcelable onSaveInstanceState() {
        SavedState v1 = new SavedState(super.onSaveInstanceState());
        v1.currentPosition = this.e;
        return ((Parcelable) v1);
    }

    public boolean onTouchEvent(MotionEvent arg3) {
        if (arg3.getAction() == 0) {
            this.v = ((int) arg3.getX());
        }

        if (2 == arg3.getAction() && this.v == 0) {
            this.v = ((int) arg3.getX());
        }

        if (1 == arg3.getAction()) {
            arg3.getX();
            this.v = 0;
        }

        return super.onTouchEvent(arg3);
    }

    public void setIsScaleSelectedTabText(boolean arg1) {
        this.i = arg1;
        this.invalidate();
    }

    public void setNightMode(boolean arg1) {
        this.c();
    }

    public void setOnPageChangeListener(ViewPager.OnPageChangeListener arg1) {
        this.a = arg1;
    }

    public void setOnTabClickListener(CategoryTabStrip$d arg1) {
        this.j = arg1;
    }

    public void setShowBottomLine(boolean arg1) {
        this.w = arg1;
        this.invalidate();
    }

    public void setStyle(Style arg1) {
        this.n = arg1;
        this.c();
    }

    public void setTabTextSize(float arg1) {
        this.h = arg1;
        this.invalidate();
    }

    public void setViewPager(ViewPager viewPager) {
        this.c = viewPager;
        if (viewPager.getAdapter() == null) {
            throw new IllegalStateException("ViewPager does not have adapter instance.");
        }
        viewPager.setOnPageChangeListener(this.m);
        this.a();
    }

    public enum Style {
        Light(0),
        Dark(1),
        Search(2),
        SearchDark(3);

        public final int value;

        private Style(int i) {
            this.value = i;
        }
    }

    public interface a {
        CategoryItem b(int arg1);
    }

    static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator<CategoryTabStrip.SavedState> CREATOR = new Parcelable.Creator<CategoryTabStrip.SavedState>() {

            public final CategoryTabStrip.SavedState createFromParcel(Parcel source) {
                return new CategoryTabStrip.SavedState(source);
            }

            public final CategoryTabStrip.SavedState[] newArray(int size) {
                return new CategoryTabStrip.SavedState[size];
            }
        };
        int currentPosition;

        public SavedState(Parcelable arg1) {
            super(arg1);
        }

        private SavedState(Parcel arg2) {
            super(arg2);
            this.currentPosition = arg2.readInt();
        }

        public void writeToParcel(Parcel arg2, int arg3) {
            super.writeToParcel(arg2, arg3);
            arg2.writeInt(this.currentPosition);
        }
    }

    public static final class CategoryTabStrip$d {
        public final BaseHomePageFragment a;

        public CategoryTabStrip$d(BaseHomePageFragment arg1) {
            this.a = arg1;
        }
    }

    final class CategoryTabStrip$b implements ViewPager.OnPageChangeListener {
        private final CategoryTabStrip a;

        CategoryTabStrip$b(CategoryTabStrip arg1, byte arg2) {
            this(arg1);
        }

        private CategoryTabStrip$b(CategoryTabStrip arg1) {
            this.a = arg1;
        }

        public final void onPageScrollStateChanged(int state) {
            if (state == 0) {
                this.a.g = false;
                if (this.a.c.getCurrentItem() == 0) {
                    this.a.scrollTo(0, 0);
                } else if (this.a.c.getCurrentItem() == this.a.d - 1) {
                    this.a.scrollTo(this.a.getScrollRange(), 0);
                } else {
                    this.a.a(this.a.c.getCurrentItem());
                }
            }

            if (this.a.a != null) {
                this.a.a.onPageScrollStateChanged(state);
            }
        }

        public final void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            this.a.e = position;
            this.a.f = positionOffset;
            if (this.a.b != null && this.a.b.getChildCount() > position) {
                CategoryTabStrip v0 = this.a;
                this.a.b.getChildAt(position).getWidth();
                v0.a(position);
                this.a.invalidate();
                if (this.a.a != null) {
                    this.a.a.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }
        }

        public final void onPageSelected(int position) {
            if (this.a.b != null) {
                int v0;
                for (v0 = 0; v0 < this.a.b.getChildCount(); ++v0) {
                    if (v0 >= 0) {
                        TextView v2 = CategoryTabStrip.a(this.a.b.getChildAt(v0));
                        if (v2 != null && (this.a.i)) {
                            float v1 = v0 == position ? this.a.h + 1f : this.a.h;
                            v2.setTextSize(v1);
                        }
                    }
                }
            }

            if (this.a.a != null) {
                this.a.a.onPageSelected(position);
            }
        }
    }

    final class c {
        TextView a;
        ImageView b;
        CategoryItem c;
        int d;

        c() {

        }
    }
}

